<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<button type= "primary" open-type="getUserInfo" @getuserinfo="getUserInfo">使用微信登录</button>
		<!-- #endif -->
	</view>
</template>

<script>
	var _self, session_key, openid, pageOptions;
	var sign = require('../../commons/sign.js');
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			getUserInfo: function(info){
				console.log(info);
				//多应用 多平台统一登录
				// var encryptedData = info.mp.detail.encryptedData;
				// var iv = info.mp.detail.iv;
				// info = info.mp.detail.userInfo;
				// uni.request({
				// 	url: _self.apiServer+'member&m=wxaes',
				// 	method: 'POST',
				// 	header: {'content-type': "application/x-www-form-urlencoded"},
				// 	data: {
				// 		session_key: session_key,
				// 		encryptedData: encryptedData,
				// 		iv: iv
				// 	},
				// 	success: res => {
				// 		console.log(res);
				// 	},
				// 	fail: () => {},
				// });
				
				info = info.mp.detail.userInfo;
				var sign = uni.getStorageSync('sign');
				uni.request({
					url: _self.apiServer+'member&m=login',
					method: 'POST',
					header: {'content-type': "application/x-www-form-urlencoded"},
					data: {
						openid: openid,
						name: info.nickName,
						face: info.avatarUrl,
						sign: sign
					},
					success: res => {
						res = res.data;
						uni.showToast({title: "登录成功"}); 
						_self.saveUserinfo(res)
					},
					fail: () => {
						
					}
				})
			}
		},
		saveUserinfo(res){
			//登录成功 记录用户信息到本地
			uni.setStorageSync('SUID', res.data.u_id + '');
			uni.setStorageSync('SRAND', res.data.u_random + '');
			uni.setStorageSync('SNAME', res.data.u_name + '');
			uni.setStorageSync('SFACE', res.data.u_face + '');
			//跳转
			if (pageOptions.backtype == 1) {
				uni.redirectTo({url: pageOptions.backpage});
			}else{
				uni.switchTab({url: pageOptions.backpage});
			}
		},
		onLoad: function(options) {
			sign.sign(this.apiServer);
			pageOptions = options;
			_self = this;
			// console.log(options);
			//微信
			// #ifdef MP-WEIXIN
			uni.login({
				success: function(res){
					//console.log(res);
					uni.request({
						url: _self.apiServer+'member&m=codeToSession&code='+ res.code,
						method: 'GET',
						success: res => {
							//console.log(res);
							session_key = res.data.session_key;
							openid = res.data.openid;
						},
						fail: () => {
							
						}
					})
				}
			})
			// #endif
			
			//app
			// #ifdef APP-PLUS
			uni.login({   //用户登录
				success: (res) => {
					//console.log(JSON.stringify(res));
					uni.getUserInfo({      //获取用户信息
						success: function(info) {
							//console.log(JSON.stringify(info));
							//与服务器交互将数据提交到服务端数据库
							var sign = uni.getStorageSync('sign');
							uni.request({
								url: _self.apiServer+'member&m=login',
								method: 'POST',
								header: {'content-type': "application/x-www-form-urlencoded"},
								data: {
									openid: info.userInfo.openId,
									name: info.userInfo.nickName,
									face: info.userInfo.avatarUrl,
									sign: sign,
								},
								success: res => {
									console.log(JSON.stringify(res));
									res = res.data;
									if(res.status == 'ok') {
										uni.showToast({title: "登录成功"});
										_self.saveUserinfo() 
									}else{
										uni.showToast({title: res.data});
									}
								}
							})
						},
						fail: () => {
							uni.showToast({title: "微信登录授权失败", icon: "none"});   //显示消息提示框  icon图标
						}
					})
				},
				fail: () => {
					uni.showToast({title: "微信登录授权失败", icon: "none"});
				}
			})
			//#endif
		}
	}
</script>

<style>

</style>
